# encoding: utf-8

class TutorialsController < ApplicationController

	before_filter :load_sidebar
	before_filter :require_moderator, :except => [:show]
	before_filter :do_index, :only => [:show]
	
	# prikaz pojedinog tutoriala
	def show
		@tutorial = Tutorial.where( :slug => params[:slug] ).first
		@meta_title = @tutorial.name + " | " + @tutorial.tutorial_category.name

		@meta_description = @tutorial.description
	end

	# obrazac za stvaranje tutoriala
	def new
		@tutorial = Tutorial.new
		@tutorial_categories = TutorialCategory.all
	end

	# obrazac za uredjivanje tutoriala
	def edit
		@tutorial = Tutorial.find(params[:id])
		@tutorial_categories = TutorialCategory.all
	end

	# stvaranje tutoriala
	def create
		@tutorial = Tutorial.new(params[:tutorial])
		@tutorial.member_id = current_member.id

		if @tutorial.save
			redirect_to(tutorial_categories_path, :notice => 'Tutorial je spremljen.')
		else
			@tutorial_categories = TutorialCategory.all
			render :action => "new" 
		end
	end

	# spremanje promjena
	def update
		@tutorial = Tutorial.find(params[:id])

		if @tutorial.update_attributes(params[:tutorial])
			redirect_to(edit_tutorial_path(@tutorial) , :notice => 'Tutorial je apdejtan arrrr :)') 
		else
			render :action => "edit" 
		end
	end

	# brisanje tutoriala
	def destroy
		@tutorial = Tutorial.find(params[:id])
		@tutorial.destroy

		redirect_to(tutorials_admin_path, :notice => 'Tutorial je izbrisan') 
	end
end
